package com.weiwei.diancan.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;

import com.weiwei.diancan.Contant.ConfigMethod;
import com.weiwei.diancan.R;
import com.weiwei.diancan.bean.LoginBean;
import com.weiwei.diancan.cache.CacheConfig;
import com.weiwei.diancan.http.RetrofitFactory;

import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import weiwei.com.weiweilibrary.views.ToastUtil;

public class WelcomeActivity extends AppCompatActivity {

    public void openActivity(Class<?> c,Bundle v){
        Intent intent = new Intent(this,c);
        if(v!=null){
            intent.putExtras(v);
        }
        startActivity(intent);
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 0:      //token为空,water_token为空
                    openActivity(LoginActivity.class,null);
                    WelcomeActivity.this.finish();
                    break;
                case 1:      //token不为空，water_token为空
                    openActivity(BangdingActivity.class,null);
                    WelcomeActivity.this.finish();
                    break;
                case 2:      ////token不为空，water_token不为空
                    onRefreshData();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        new Thread(){
            @Override
            public void run() {
                try {
                    CacheConfig inst = CacheConfig.getInst();
                    String token = inst.getToken(WelcomeActivity.this);
                    String waterToken = inst.getWaterToken(WelcomeActivity.this);
                    Thread.sleep(3000);

                    int type = 0;
                    if(TextUtils.isEmpty(token)){
                        type = 0;
                    }else{
                        if(TextUtils.isEmpty(waterToken)){
                            type = 1;
                        }else{
                            type = 2;
                        }
                    }

                    handler.sendEmptyMessage(type);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    /**
     *获取数据
     */
    private void onRefreshData() {

        Map<String, String> map = new HashMap<>();
        map.put(ConfigMethod.shop_token, CacheConfig.getInst().getToken(this));
        map.put(ConfigMethod.waiter_token, CacheConfig.getInst().getWaterToken(this));
        RetrofitFactory.getIntance().refreshData(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<LoginBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUtil.showToast(WelcomeActivity.this,"请检查您的网络");
                        WelcomeActivity.this.finish();
                    }

                    @Override
                    public void onNext(LoginBean loginBean) {
                        if(loginBean.getIs_success().equals(ConfigMethod.Success)) {
                            loginBean.setShop_token(CacheConfig.getInst().getToken(WelcomeActivity.this));
                            CacheConfig.getInst().putData(loginBean);
                            openActivity(Main1Activity.class,null);
                            WelcomeActivity.this.finish();
                        }else{
                            ToastUtil.showToast(WelcomeActivity.this,loginBean.getReason());
                            if(loginBean.getIs_success().equals("-1")){
                                openActivity(LoginActivity.class,null);
                            }else if(loginBean.getIs_success().equals("-2")){
                                openActivity(BangdingActivity.class,null);
                            }
                            WelcomeActivity.this.finish();
                        }
                    }
                });
    }
}
